package com.tnt.webpageauthorization.demo;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author zhuangxl
 * @description
 * @since 2021-03-04 11:13
 */
@RestController
@RequestMapping("/webpage/authorization")
public class WebpageAuthorizationController {


    /**
     * @description: 静默网页授权，获取openid
     * @author: zhuangxl
     * @since: 21/3/4 12:58
     * @param code: 
     * @param state: 
     * @return: java.lang.String
     **/
    @RequestMapping("/snsapi_base")
    public WxWebPageAccessToken snsapi_base(@RequestParam("code")String code, @RequestParam("state")String state){

        WxWebPageAccessToken wxWebPageAccessToken = new WxWebPageAccessToken();

        try {
            wxWebPageAccessToken = WechatUtil.getAccessTokenAndOpenid(code);
            System.out.println(wxWebPageAccessToken.toString());

        } catch (Exception e) {
            e.printStackTrace();
        }

        return wxWebPageAccessToken;
    }

    /**
     * @description: 手动网页授权，获取用户的基本信息
     * @author: zhuangxl
     * @since: 21/3/4 12:58
     * @param code: 
     * @param state: 
     * @return: java.lang.String
     **/
    @RequestMapping("/snsapi_userinfo")
    public WxWebPageUserinfo snsapi_userinfo(@RequestParam("code")String code, @RequestParam("state")String state){

        WxWebPageUserinfo wxWebPageUserinfo = new WxWebPageUserinfo();

        try {
            WxWebPageAccessToken wxWebPageAccessToken = WechatUtil.getAccessTokenAndOpenid(code);
            System.out.println(wxWebPageAccessToken.toString());

            wxWebPageUserinfo = WechatUtil.getUserinfo(wxWebPageAccessToken.getAccess_token(), wxWebPageAccessToken.getOpenid());

            System.out.println(wxWebPageUserinfo.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }

        return wxWebPageUserinfo;
    }

}
